//
//  TYZSearchBar.m
//  TYZ_SC
//
//  Created by 李贤立 on 2017/12/12.
//  Copyright © 2017年 Capfer. All rights reserved.
//

#import "TYZSearchBar.h"

@implementation TYZSearchBar

- (instancetype)init {
    if (self = [super init]) {
        self.backgroundColor = TYZClearColor;
        self.showsCancelButton = NO;
        self.tintColor = [UIColor orangeColor];
        for (UIView *subView in self.subviews) {
            if ([subView isKindOfClass:[UIView  class]]) {
                [[subView.subviews objectAtIndex:0] removeFromSuperview];
                if (subView.subviews.count > 0) {
                    if ([[subView.subviews objectAtIndex:0] isKindOfClass:[UITextField class]]) {
                        UITextField *textField = [subView.subviews objectAtIndex:0];
                        textField.backgroundColor = TYZSameColor(240);
                        
                        //设置默认文字颜色
                        UIColor *color = [UIColor grayColor];
                        [textField setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:[@"搜索感兴趣的内容" S] attributes:@{NSForegroundColorAttributeName:color}]];
                        textField.font = [UIFont systemFontOfSize:CoreInfo.fontSize * 0.8];
                        //修改默认的放大镜图片
                        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
                        imageView.backgroundColor = [UIColor clearColor];
                        imageView.image = [UIImage imageNamed:@"sousuo"];
                        textField.leftView = imageView;
                    }
                } else {
                    self.placeholder = [@"搜索感兴趣的内容" S];
                    [self setImage:[UIImage imageNamed:@"sousuo"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
                }
            }
        }
    }
    return self;
}
- (instancetype)initWithWrittenSearch {
    if (self = [super init]) {
        self.backgroundColor = TYZWrittenBGColor;
        self.showsCancelButton = NO;
        self.tintColor = [UIColor orangeColor];
        [self setImage:[UIImage imageNamed:@"sousuo"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
        for (UIView *subView in self.subviews) {
            if ([subView isKindOfClass:[UIView  class]]) {
                [[subView.subviews objectAtIndex:0] removeFromSuperview];
            }
        }
    }
    return self;
}
- (instancetype)initWithPlaceholder:(NSString *)placeholder {
    if (self = [super init]) {
        self.backgroundColor = TYZClearColor;
        self.showsCancelButton = NO;
        self.tintColor = TYZSameColor(51);
        for (UIView *subView in self.subviews) {
            if ([subView isKindOfClass:[UIView  class]]) {
                [[subView.subviews objectAtIndex:0] removeFromSuperview];
                if (subView.subviews.count > 0) {
                    if ([[subView.subviews objectAtIndex:0] isKindOfClass:[UITextField class]]) {
                        UITextField *textField = [subView.subviews objectAtIndex:0];
                        textField.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
                        textField.returnKeyType = UIReturnKeyDone;
                        //  设置输入框边框的颜色
                        //                textField.layer.borderColor = [UIColor blackColor].CGColor;
                        //                textField.layer.borderWidth = 1;
                        
                        //  设置输入字体颜色/Users/lihua/Downloads/che2@3x.png
                        //                textField.textColor = [UIColor lightGrayColor];
                        
                        //设置默认文字颜色
                        UIColor *color = [UIColor grayColor];
                        [textField setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:self.placeholder attributes:@{NSForegroundColorAttributeName:color}]];
                        textField.font = [UIFont systemFontOfSize:CoreInfo.fontSize * 0.8];
                        //修改默认的放大镜图片
                        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
                        imageView.backgroundColor = [UIColor clearColor];
                        imageView.image = [UIImage imageNamed:@"sousuo"];
                        textField.leftView = imageView;
                    }
                } else {
                    self.placeholder = placeholder;
                    [self setImage:[UIImage imageNamed:@"sousuo"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
                }
            }
        }
    }
    return self;
}
@end
